package com.dingwei.superiordiary.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ViewPagerAdapter;
import com.dingwei.superiordiary.bean.TabFragmentTitleItem;
import com.dingwei.superiordiary.mvp.presenter.DynamicPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.PostSearchEditActivity;
import com.dingwei.superiordiary.ui.activity.ScanActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class DynamicFragment extends LazyLoadFragment implements Contract.DynamicragmentView {
    DynamicPresenter dynamicPresenter = new DynamicPresenter(this);
    @Bind(R.id.fake_status_bar)
    View fakeStatusBar;
    @Bind(R.id.search_layout)
    LinearLayout searchLayout;
    @Bind(R.id.img_scan)
    ImageView imgScan;
    @Bind(R.id.ll_search)
    LinearLayout llSearch;
    @Bind(R.id.order_tablayout)
    TabLayout orderTablayout;
    @Bind(R.id.m_viewpager)
    ViewPager mViewpager;

    private List<Fragment> list = new ArrayList<>();

    private List<TabFragmentTitleItem> fragmentTitles = new ArrayList<>();
    /**
     * 设置状态栏高度 主要是设配刘海屏手机
     */
    private void setStatusBarHeight() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getStatusHeight(BaseActivity.context));
        fakeStatusBar.setLayoutParams(lp);
    }
    @Override
    protected int setContentView() {
        return R.layout.fragment_dynamic;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView() {

        setStatusBarHeight();

        TabFragmentTitleItem item = new TabFragmentTitleItem("关注", "3", "");
        fragmentTitles.add(item);
        item = new TabFragmentTitleItem("精选", "1", "");
        fragmentTitles.add(item);
        item = new TabFragmentTitleItem("综合", "2", "");
        fragmentTitles.add(item);


        for (int i = 0; i < fragmentTitles.size(); i++) {
            IndexAttentionFragment fragment = new IndexAttentionFragment();
            Bundle bundle = new Bundle();
            bundle.putString("id", fragmentTitles.get(i).id);
            fragment.setArguments(bundle);
            list.add(fragment);
        }


        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        adapter.setmFragments(list, fragmentTitles);
        mViewpager.setAdapter(adapter);
        mViewpager.setOffscreenPageLimit(fragmentTitles.size());
        orderTablayout.setupWithViewPager(mViewpager);

        mViewpager.setCurrentItem(0);

    }

    @Override
    protected void lazyLoad() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.search_layout, R.id.img_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_layout:
                intent = new Intent(BaseActivity.context, PostSearchEditActivity.class);
                startActivity(intent);
                break;
            case R.id.img_scan:
                intent = new Intent(BaseActivity.context, ScanActivity.class);
                startActivity(intent);
                break;
        }
    }
}
